*this do-file constructs the regression dataset with all HS=1/0 and NON
use "$out/potential_controls_updated_tlm_bnoSext2_SL5_d3_6_in_EMP_2023Nov22.dta", clear
gen HS=0
append using "$out/IVdata_HS1_accidents_bnoSext2_SL5_2022Sept26.dta"
replace HS=1 if HS==.

keep anon HS ABS ABSd mint_clu maxt_clue flagEMPm0 flagEMPml1

*save "$out/future_labor_m3_2023Nov22.dta", replace 
tempfile future_labor_m3_2023Nov22
save `future_labor_m3_2023Nov22'

use "$out/potential_controls_updated_tlm_bnoSext2_SL5_d3_6_in_EMP_2023Nov22.dta", clear
gen HS=0
append using "$out/IVdata_HS1_accidents_bnoSext2_SL5_2022Sept26.dta"
recode HS (.=1)

duplicates report anon t // no duplicates //

	keep anon t 
	
	tempfile shock_time
	save `shock_time'
	*use "/homeKRTK/health_shocks/future_labor_m3_2023Nov22.dta", clear
	use `future_labor_m3_2023Nov22', clear
	
	merge 1:m anon using "$in/admin3_alap.dta", keepusing(t) keep(master match) nogen
	merge 1:1 anon t using `shock_time'
		
	gen tflag=t if _m==3 // flags the time of the "ACC"
	drop _m
	
	merge 1:1 anon t using "$in/admin3_passziv.dta", keepusing(passziv_tip1) keep(master match)
	assert passziv_tip1!=. if _merge==3
	drop _merge
	rename passziv_tip1 sick_leave_days
*save "/homeKRTK/health_shocks/IVtemp_2023Nov22.dta", replace
tempfile IVtemp_2023Nov22 
save `IVtemp_2023Nov22', replace

*use "/homeKRTK/health_shocks/IVtemp_2023Nov22.dta", clear
use `IVtemp_2023Nov22'

preserve
keep anon
duplicates drop
tempfile people
save `people'
restore

merge 1:1 anon t using "$in/admin3_alap.dta", keepusing(vallazon1 w1 wh1 fogvisz1 nap jnap  transfer_mtp transfer_ny teaor1_h1 feor1_h2 wtip) keep(master match) nogen

gen     EMP = 1 if (vallazon1!=.) & ((w1!=. & w1>0) | (wh1!=. & wh1>0)) & (sick_leave_days<6 | sick_leave_days==.) & (fogvisz1==201)
egen maxtflag = max(tflag), by(anon)
replace maxtflag = maxt_clue if maxt_clu!=.
gen m = t-maxtflag

rename sick_leave_days SLdays
	
capture drop _m
foreach num of numlist 1/12 {
	gen tmerge_t`num' = maxtflag+`num'
	foreach var of varlist w1 wh1 wtip1 nap jnap vallazon1 transfer_mtp transfer_ny SLdays fogvisz1 feor1_h2 teaor1_h1 {
		gen `var'_`num'_temp=`var' if t==tmerge_t`num'
	}
	preserve
		keep if t==tmerge_t`num'
		keep anon w1_`num'_temp wh1_`num'_temp wtip1_`num'_temp  nap_`num'_temp jnap_`num'_temp vallazon1_`num'_temp transfer_mtp_`num'_temp transfer_ny_`num'_temp SLdays_`num'_temp fogvisz1_`num'_temp feor1_h2_`num'_temp teaor1_h1_`num'_temp 
		foreach x in w1 wh1 wtip1 nap jnap vallazon1 transfer_mtp transfer_ny SLdays fogvisz1 feor1_h2 teaor1_h1 {
			rename `x'_`num'_temp `x'_t`num'
			}		
		sort anon

		merge 1:1 anon using `people'
		drop _merge
		save `people', replace
		
	restore
	foreach x in w1 wh1 wtip1 nap jnap vallazon1 transfer_mtp transfer_ny SLdays fogvisz1 feor1_h2 teaor1_h1 {
		drop `x'_`num'_temp
	}
}

capture drop _m
* forward values, from d onwards * 
gen d = ABSd
foreach num of numlist 0 6 8 10 12 14 16 18 20 22 24 26 28 30 {
	gen tmerge_td`num' = maxtflag+d+`num'
	foreach var of varlist w1 wh1 wtip1 nap jnap vallazon1 transfer_mtp transfer_ny SLdays fogvisz1 feor1_h2 teaor1_h1 {
		gen `var'_`num'_temp=`var' if t==tmerge_td`num'
	}
	preserve
		keep if t==tmerge_td`num'
		keep anon w1_`num'_temp wh1_`num'_temp wtip1_`num'_temp  nap_`num'_temp jnap_`num'_temp vallazon1_`num'_temp transfer_mtp_`num'_temp transfer_ny_`num'_temp SLdays_`num'_temp fogvisz1_`num'_temp feor1_h2_`num'_temp teaor1_h1_`num'_temp 
		foreach x in w1 wh1 wtip1 nap jnap vallazon1 transfer_mtp transfer_ny SLdays fogvisz1 feor1_h2 teaor1_h1 {
			rename `x'_`num'_temp `x'_td`num'
			}		
		sort anon

		merge 1:1 anon using `people'
		drop _merge
		save `people', replace
		
	restore
	foreach x in w1 wh1 wtip1 nap jnap vallazon1 transfer_mtp transfer_ny SLdays fogvisz1 feor1_h2 teaor1_h1 {
		drop `x'_`num'_temp
	}
}

* for clustered shocks take the first month of the shock as a baseline for getting the pre-shock months
replace maxtflag = mint_clu if mint_clu!=. 

* lagged values, prior to m=0 *
foreach num of numlist 0/12 {
	gen tmerge_l`num' = maxtflag-`num'
	foreach var of varlist w1 wh1 wtip1 nap jnap vallazon1 transfer_mtp transfer_ny SLdays fogvisz1 feor1_h2 teaor1_h1 {
		gen `var'_`num'_temp=`var' if t==tmerge_l`num'
	}
	preserve
		keep if t==tmerge_l`num'
		keep anon w1_`num'_temp wh1_`num'_temp wtip1_`num'_temp nap_`num'_temp jnap_`num'_temp vallazon1_`num'_temp transfer_mtp_`num'_temp transfer_ny_`num'_temp SLdays_`num'_temp fogvisz1_`num'_temp feor1_h2_`num'_temp teaor1_h1_`num'_temp
		foreach x in w1 wh1 wtip1 nap jnap vallazon1 transfer_mtp transfer_ny SLdays fogvisz1 feor1_h2 teaor1_h1 {
			rename `x'_`num'_temp `x'_l`num'
			}		
		sort anon
		
		merge 1:1 anon using `people'
		drop _merge
		save `people', replace
		
	restore
	foreach x in w1 wh1 wtip1 nap jnap vallazon1 transfer_mtp transfer_ny SLdays fogvisz1 feor1_h2 teaor1_h1 {
		drop `x'_`num'_temp
	}
}

* as everything is merged to each observation of an anon, kept only the newly created variables at the anon level (i.e. one observation for each) 
keep if t==tflag
merge 1:1 anon using `people'
drop _merge
keep anon t EMP tflag SLdays d w1_t* wh1_t* wtip1_t* nap_t* jnap_t* vallazon1_t* transfer_mtp_t* transfer_ny_t* SLdays_t* ///
	fogvisz1_t* feor1_h2_t* teaor1_h1_t* *_l? *_l?? 

*save "/homeKRTK/health_shocks/IVdata_accidents_bnoSext_temp2_2023Nov22.dta", replace
tempfile IVdata_accidents_bnoSext_temp2
save `IVdata_accidents_bnoSext_temp2', replace

*use "/homeKRTK/health_shocks/IVdata_accidents_bnoSext_temp2_2023Nov22.dta", clear
use `IVdata_accidents_bnoSext_temp2', clear

merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_alap.dta", keepusing(ferfi) keep(master match) nogen

egen agroup=group(ferfi t d)
gen tmerge_t = t

* health indicators *

forval i=1(1)12 {
capture drop t
	gen t=tmerge_t-`i'
	
	merge 1:1 anon t using "$in/admin3_eu_fekvo.dta", nogen keepusing(fekvo_ft) keep(master match)
	
	merge 1:1 anon t using "$in/admin3_eu_jaro.dta", nogen keepusing(jaro_ft_ossz) keep(master match)
	
	merge 1:1 anon t using "$in/admin3_eu_veny.dta", nogen keepusing(tbtam_ossz betegft_ossz) keep(master match)
	
	merge 1:1 anon t using "$in_med/admin3_eu_veny_H2_v2.dta", nogen keepusing(A_ft B_ft C_ft D_ft G_ft H_ft J_ft L_ft M_ft N_ft P_ft R_ft S_ft V_ft) keep(master match)
	
	foreach var of varlist fekvo_ft jaro_ft_ossz tbtam_ossz betegft_ossz A_ft B_ft C_ft D_ft G_ft H_ft J_ft L_ft M_ft N_ft P_ft R_ft S_ft V_ft {
		rename `var' `var'_m`i'
	}
	
	drop t
}

forval i=1(1)12 {
capture drop t
	gen t=tmerge_t+`i'
	
	merge 1:1 anon t using "$in/admin3_eu_fekvo.dta", nogen keepusing(fekvo_ft) keep(master match)
	
	merge 1:1 anon t using "$in/admin3_eu_jaro.dta", nogen keepusing(jaro_ft_ossz) keep(master match)
	
	merge 1:1 anon t using "$in/admin3_eu_veny.dta", nogen keepusing(tbtam_ossz betegft_ossz) keep(master match)
	
	merge 1:1 anon t using "$in_med/admin3_eu_veny_H2_v2.dta", nogen keepusing(A_ft B_ft C_ft D_ft G_ft H_ft J_ft L_ft M_ft N_ft P_ft R_ft S_ft V_ft) keep(master match)
	
	foreach var of varlist fekvo_ft jaro_ft_ossz tbtam_ossz betegft_ossz A_ft B_ft C_ft D_ft G_ft H_ft J_ft L_ft M_ft N_ft P_ft R_ft S_ft V_ft {
		rename `var' `var'_t`i'
	}
	
	drop t
}

*create composite indicators
*total health spending 3-12 months ago
gen health_12mo=0
forval i=3(1)12 {
	foreach var of varlist fekvo_ft_m`i' jaro_ft_ossz_m`i' tbtam_ossz_m`i' betegft_ossz_m`i' {
		replace `var'=0 if `var'==.
	}
	gen health_m`i' = fekvo_ft_m`i' + jaro_ft_ossz_m`i' + tbtam_ossz_m`i' + betegft_ossz_m`i'

	replace health_12mo=health_12mo+health_m`i'
}
gen log_health_12mo=log(health_12mo)
replace log_health_12mo=0 if log_health_12mo==.
 
*total health spending from t=1 to t=d-1
gen health_ind=0
forval i=1(1)12 {
	foreach var of varlist fekvo_ft_t`i' jaro_ft_ossz_t`i' tbtam_ossz_t`i' betegft_ossz_t`i' {
		replace `var'=0 if `var'==.
	}
	gen health_t`i' = fekvo_ft_t`i' + jaro_ft_ossz_t`i' + tbtam_ossz_t`i' + betegft_ossz_t`i'

	replace health_ind=health_ind+health_t`i' if d>`i'
}
gen log_health_ind=log(health_ind)
replace log_health_ind=0 if log_health_ind==. 

*binary indicators of spending on drug categories 3-12 months ago
gen A_12mo=0
gen B_12mo=0
gen C_12mo=0
gen D_12mo=0
gen G_12mo=0
gen H_12mo=0
gen J_12mo=0
gen L_12mo=0
gen M_12mo=0
gen N_12mo=0
gen P_12mo=0
gen R_12mo=0
gen S_12mo=0
gen V_12mo=0

forval i=3(1)12 {
	foreach var of varlist A_ft_m`i' B_ft_m`i' C_ft_m`i' D_ft_m`i' G_ft_m`i' H_ft_m`i' J_ft_m`i' L_ft_m`i' M_ft_m`i' N_ft_m`i' P_ft_m`i' R_ft_m`i' S_ft_m`i' V_ft_m`i' {
		replace `var'=0 if `var'==.
		replace `var'=1 if `var'>0 & `var'~=.
	}
	
	replace A_12mo=1 if A_ft_m`i'==1
	replace B_12mo=1 if B_ft_m`i'==1
	replace C_12mo=1 if C_ft_m`i'==1
	replace D_12mo=1 if D_ft_m`i'==1
	replace G_12mo=1 if G_ft_m`i'==1
	replace H_12mo=1 if H_ft_m`i'==1
	replace J_12mo=1 if J_ft_m`i'==1
	replace L_12mo=1 if L_ft_m`i'==1
	replace M_12mo=1 if M_ft_m`i'==1
	replace N_12mo=1 if N_ft_m`i'==1
	replace P_12mo=1 if P_ft_m`i'==1
	replace R_12mo=1 if R_ft_m`i'==1
	replace S_12mo=1 if S_ft_m`i'==1
	replace V_12mo=1 if V_ft_m`i'==1
	
}

*binary indicators of spending on drug categories from t=1 to d-1
gen A_ind=0
gen B_ind=0
gen C_ind=0
gen D_ind=0
gen G_ind=0
gen H_ind=0
gen J_ind=0
gen L_ind=0
gen M_ind=0
gen N_ind=0
gen P_ind=0
gen R_ind=0
gen S_ind=0
gen V_ind=0

forval i=3(1)12 {
	foreach var of varlist A_ft_t`i' B_ft_t`i' C_ft_t`i' D_ft_t`i' G_ft_t`i' H_ft_t`i' J_ft_t`i' L_ft_t`i' M_ft_t`i' N_ft_t`i' P_ft_t`i' R_ft_t`i' S_ft_t`i' V_ft_t`i' {
		replace `var'=0 if `var'==. & d>`i'
		replace `var'=1 if `var'>0 & `var'~=. & d>`i'
	}
	
	replace A_ind=1 if A_ft_t`i'==1 & d>`i'
	replace B_ind=1 if B_ft_t`i'==1 & d>`i'
	replace C_ind=1 if C_ft_t`i'==1 & d>`i'
	replace D_ind=1 if D_ft_t`i'==1 & d>`i'
	replace G_ind=1 if G_ft_t`i'==1 & d>`i'
	replace H_ind=1 if H_ft_t`i'==1 & d>`i'
	replace J_ind=1 if J_ft_t`i'==1 & d>`i'
	replace L_ind=1 if L_ft_t`i'==1 & d>`i'
	replace M_ind=1 if M_ft_t`i'==1 & d>`i'
	replace N_ind=1 if N_ft_t`i'==1 & d>`i'
	replace P_ind=1 if P_ft_t`i'==1 & d>`i' 
	replace R_ind=1 if R_ft_t`i'==1 & d>`i'
	replace S_ind=1 if S_ft_t`i'==1 & d>`i'
	replace V_ind=1 if V_ft_t`i'==1 & d>`i'
	
}

keep anon *ind *mo *health* *_ft_* tbtam* betegft* tmerge_t agroup 

*save "/homeKRTK/health_shocks/IVdata_accidents_bnoSext_temp3_2023Nov22.dta" , replace
tempfile IVdata_accidents_bnoSext_temp3
save `IVdata_accidents_bnoSext_temp3'

*use "/homeKRTK/health_shocks/IVdata_accidents_bnoSext_temp3_2023Nov22.dta", clear
use `IVdata_accidents_bnoSext_temp3', clear
gen t = tmerge_t // ==tflag, time of the shock
gen ev=2003+floor((t-1)/12)

merge 1:1 anon t using `IVtemp_2023Nov22', keep(master match) nogen keepusing(maxt_clue flagEMPml1)
merge 1:1 anon using `IVdata_accidents_bnoSext_temp2', nogen keep(master match) keepusing(d vallazon1_*)
merge 1:1 anon t using "$in/admin3_alap.dta", keepusing(vallazon1) keep(master match) nogen
gen maxtflag=t
replace maxtflag = maxt_clue if maxt_clu!=.

foreach num of numlist 0 6 8 10 12 14 16 18 20 22 24 26 28 30 {
	gen td`num' = maxtflag+d+`num'
}
foreach num of numlist 1/12 {
	gen l`num' = t-`num'
	}
foreach num of numlist 1/12 {
	gen t`num' = maxtflag+`num'
}
	
foreach x in td0 td6 td8 td10 td12 td14 td16 td18 td20 td22 td24 td26 td28 td30 l1 ///
	l2 l3 l4 l5 l6 l7 l8 l9 l10 l11 l12 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 {
	rename t t_temp
	rename `x' t
	rename vallazon1_`x' vallazon
	merge m:1 vallazon ev using "$out/firmquality.dta", nogen keep(master match)
	foreach var of varlist letszam mean_firmw FO mean_tfp_wr tfp_wr vallazon {
		rename `var' `var'_`x'
	}
	drop t
	rename t_temp t
}

foreach num of numlist 6 8 10 12 14 16 18 20 22 24 26 28 30 {
	rename vallazon_td`num' vallazon 
	capture drop _m
	merge m:1 vallazon using "$out_AKM/FEj_full_Jan2023.dta", 
	drop if _m==2
	drop _m
	rename vallazon vallazon1_td`num' 
	rename vall_fix FEj
	foreach var of varlist FEj {
		rename `var' `var'_td`num'
	}	
}
foreach num of numlist 1/12 {
	rename vallazon_t`num' vallazon 
	capture drop _m
	merge m:1 vallazon using "$out_AKM/FEj_full_Jan2023.dta", 
	drop if _m==2
	drop _m
	rename vallazon vallazon1_t`num' 
	rename vall_fix FEj
	foreach var of varlist FEj {
		rename `var' `var'_t`num'
	}	
}

gen ttemp=t

rename vallazon1 vallazon
replace t = t-1 if flagEMPml1==1

merge m:1 vallazon ev using "$out/firmquality.dta", nogen keep(master match)
foreach var of varlist mean_firmw FO mean_tfp_wr tfp_wr vallazon letszam{
	rename `var' `var'_t
}

rename vallazon_t vallazon
merge m:1 vallazon using "$out_AKM/FEj_full_Jan2023.dta", nogen keep(master match)
rename vall_fix FEj_t

rename vallazon vallazon_t

foreach x in td0 l1 l2 l3 l4 l5 l6 l7 l8 l9 l10 l11 l12 {
	rename vallazon_`x' vallazon 
	capture drop _m
	merge m:1 vallazon using "$out_AKM/FEj_full_Jan2023.dta", 
	drop if _m==2
	drop _m
	rename vallazon vallazon1_`x' 
	rename vall_fix FEj
	foreach var of varlist FEj {
		rename `var' `var'_`x'
	}	
}

sort anon
merge m:1 anon using "$out/AKM-FEs/FEi_full_Jan2023.dta"
tab _m
drop if _m==2
drop _m

rename anon_fix FEi

merge 1:1 anon t using `IVtemp_2023Nov22', keep(master match) nogen keepusing(ABS ABSd HS mint_clu flagEMPm0)
merge 1:1 anon t using "$in/admin3_alap.dta", keepusing(w1 wh1 nap ferfi kor teaor1_h1 feor1_h2 transfer_mtp transfer_ny fogvisz1 wtip id_tip jnap) keep(master match) nogen
merge 1:1 anon using `IVdata_accidents_bnoSext_temp2', nogen keep(master match) 

sum
save "$out/IVdata_accidents_bnoSext2_SL5_2023Nov22.dta", replace
